

import api_config from './api_config.js'

import Request from '@/utils/luch/index.js';

 const http = new Request();

 let api  = {
	
	login:function(params,callback,showloding){
		return this.httpRequest(api_config.http.login.login, params, callback, showloding)
	},
	
	bookInfo:function(params,callback,showloding){
		return this.httpRequest(api_config.http.index.bookInfo, params, callback, showloding)
	},
	
	mockTest:function(params,callback,showloding){
		return this.httpRequest(api_config.http.test.testmock, params, callback, showloding)
	},
	
	
	/**
	 * httpRequest方法，基于luch-request简单封装，整合全部请求配置
	 * url  请求的接口
	 * params 请求的参数，token可带在里面或者在request里面添加paramas参数,主要看后台的写法,或者使用拦截器添加token
	 * callback 请求数据的回调
	 * method 请求方法必须大写 GET|POST  默认post
	 * showloding 是否显示加载图标,默认为 true
	 * header 请求头，luch-request 已经配置部分，在局部参数配置会覆盖全局的配置*/
	httpRequest:function(url, params, callback, method='POST', showloding = true, header={"Content-Type":"application/x-www-form-urlencoded"})
	{
		 if(showloding) uni.showLoading({title: '加载中...'});
		  
			http.request({
				url: url,
				data: params,
				method: method, 
				header:header,    
				timeout: 5000,  
		    }).then(res => {
				//默认返回的格式是json, 返回的res.data 已经进行JSON.parse
				 if(showloding) uni.hideLoading();
				 var result = res.data
				 console.log('params:',JSON.stringify(params))
				 console.log('url:',url)
				 console.log('result:',result)	
				 
				     return callback(0, result.msg, result.data)			 
				 // if(result.code == 200){
				 //	 return callback(0, result.msg, result.data)
				 // }else{
					//  this.tokenErr()
					//  return callback(0, result.msg)
				 // }
			  }).catch(err => {
				if(showloding) uni.hideLoading();
                uni.showToast({
                    icon: 'none',
                    title: '请求失败' + JSON.stringify(err),
                });
			  })
				
	},	
	
	/**
	 * token错误时，重新授权*/
	tokenErr: function() {	
		uni.showToast({
			title: '刷新登陆中...',
			icon: 'success',
			duration: 1000,
			success: function () {				
				setTimeout(function() {		
					// h5跳转授权地址重新授权
					/*
					self.location.href= config.http.oauth.wechatUrl
					*/
				}, 1000);				
			}				
		});		
		return
	}
	
};

module.exports = {
	api:api
}